layui.use(['table','form','jquery'], function() {
    var table = layui.table,
        $ = layui.jquery,
        form = layui.form;

    var tabInstance = table.render({
        elem: '#userList',
        id:'idTest',
        url: '/sys-user/list', //数据接口
        page: true, //开启分页
        limit: 5,
        limits:[5,10,15,20],
        request:{
            pageName:'current',
            limitName:'size'
        },
        cols: [[ //表头
            {field: 'userId', title: '用户编号', sort: true, fixed: 'left', align: 'center'},
            {field: 'userName', title: '用户名', align: 'center'},
            {field: 'pwd', title: '密码', align: 'center', hide: true},
            {field: 'part', title: '角色', align: 'center',templet:function (user) {
                    switch (user.part){
                        case 1:
                            return "系统管理员";
                            break;
                        case 2:
                            return "高管";
                            break;
                        case 3:
                            return "普通员工";
                            break;
                    }
                }},
            {field: 'telephone', title: '联系电话', align: 'center'},
            {field: 'email', title: '电子邮箱', align: 'center'},
            {field: 'operation',title: '操作',toolbar:"#userTool", align: 'center', width: 290}
        ]],
        done: function () {
            $.post('/check/check-role',function (info) {
                if(info.part!=1){
                    $(".add_btn").hide();
                    $("[data-field='operation']").find('#reset_btn').attr("disabled","disabled");
                    $("[data-field='operation']").find('#reset_btn').attr("class","layui-btn layui-btn-disabled layui-btn-sm layui-icon layui-icon-edit");
                    $("[data-field='operation']").find('#edit_btn').attr("disabled","disabled");
                    $("[data-field='operation']").find('#edit_btn').attr("class","layui-btn layui-btn-disabled layui-btn-sm layui-icon layui-icon-edit");
                    $("[data-field='operation']").find('#del_btn').attr("disabled","disabled");
                    $("[data-field='operation']").find('#del_btn').attr("class","layui-btn layui-btn-disabled layui-btn-sm layui-icon layui-icon-delete");
                }
            })
        }
    });

    form.on('submit(search)',function(data){
        console.log(data);
        tabInstance.reload({
            where :data.field
        });
        return false;
    });

    //添加
    $(".add_btn").click(function () {

        layer.open({
            type: 2,
            content: 'views/user/add.jsp',
            skin:'layui-layer-molv',
            title:"添加员工",
            area:["430px","450px"],
            btn:['确定','关闭'],
            yes: function (index,layero) {
                var user_add = layer.getChildFrame('body',index).find('#user_add');
                var data = {};
                user_add.serializeArray().forEach(function (val) {
                    data[val.name] = val.value;
                });
                if (data["userName"]!="" && data["part"]!=""){
                    if (data["email"]!=""){
                        var re = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
                        if (!re.test(data["email"])){
                            layer.msg("请输入正确的邮箱格式！")
                            user_add.find(".email").val("");
                        }else {
                            $.post('/sys-user/save-update',data,function (info) {
                                layer.close(index);
                                layer.msg(info.msg);
                                tabInstance.reload();
                            })
                        }
                    }else {
                        $.post('/sys-user/save-update',data,function (info) {
                            layer.close(index);
                            layer.msg(info.msg);
                            tabInstance.reload();
                        })
                    }

                }else{
                    layer.msg("请输入【用户名】和【用户角色】！");
                }
            }
        })
    });

    table.on('tool(userList)',function (obj) {
        switch (obj.event){
            case 'reset_btn':
                layer.confirm('你确定要重置用户【'+obj.data.userName+'】的密码吗？',{title:'提示',icon:3,anim:5,skin:'layui-layer-movl'},function () {
                   $.post('/sys-user/reset/'+obj.data.userId,function (info) {
                       tabInstance.reload();
                       layer.msg(info.msg);
                   })
                });
                break;
            case 'edit_btn':
                layer.open({
                    type: 2,
                    content: 'views/user/add.jsp?part='+obj.data.part,
                    skin:'layui-layer-molv',
                    title:"添加员工",
                    area:["430px","450px"],
                    btn:['确定','关闭'],
                    success:function (layero,index) {
                        var user_add = layer.getChildFrame('body',index).find('#user_add');
                        user_add.find(".user_id").val(obj.data.userId);
                        user_add.find(".user_name").val(obj.data.userName);
                        user_add.find(".telephone").val(obj.data.telephone);
                        user_add.find(".email").val(obj.data.email);
                    },
                    yes: function (index,layero) {
                        var user_add = layer.getChildFrame('body',index).find('#user_add');
                        var data = {};
                        user_add.serializeArray().forEach(function (val) {
                             data[val.name] = val.value;
                         });
                        if (data["userName"]!="" && data["part"]!=""){
                            if (data["email"]!=""){
                                var re = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
                                if (!re.test(data["email"])){
                                    layer.msg("请输入正确的邮箱格式！")
                                    user_add.find(".email").val("");
                                }else {
                                    $.post('/sys-user/save-update',data,function (info) {
                                        layer.close(index);
                                        layer.msg(info.msg);
                                        tabInstance.reload();
                                    })
                                }
                            }else {
                                $.post('/sys-user/save-update',data,function (info) {
                                    layer.close(index);
                                    layer.msg(info.msg);
                                    tabInstance.reload();
                                })
                            }
                        }else {
                             layer.msg("请输入【用户名】和【用户角色】！")
                        }
                    }
                });
                break;
            case 'del_btn':
                layer.confirm('你确定要删除用户【'+obj.data.userName+'】吗？',{title:'提示',icon:3,anim:5,skin:'layui-layer-movl'},function () {
                    $.post('/sys-user/del/'+obj.data.userId,function (info) {
                        tabInstance.reload();
                        layer.msg(info.msg);
                    })
                })
        }
    })

})